package com.sphd.restaurantsystem.activitys.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.SelectCouponsAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.info.CouponInfo;
import com.sphd.restaurantsystem.utils.AppManager;
import com.sphd.restaurantsystem.utils.Constant;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectCouponsActivity extends BaseActivity {

    @BindView(R.id.choose_over)
    TextView chooseOver;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_selectcoupinsnum)
    TextView tvSelectcoupinsnum;
    @BindView(R.id.tv_allmoney)
    TextView tvAllmoney;
    @BindView(R.id.lv_selectcoupons)
    ListView lvSelectcoupons;
    @BindView(R.id.content_select_coupons)
    LinearLayout contentSelectCoupons;
    private double quanmoney = 0;//消费金额
    private double cutmoney = 0;
    Intent intent;
    Bundle bundle;
    double allMoney;
    List<CouponInfo> couponInfos;
    SelectCouponsAdapter adapter;
    String gmtTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_coupons);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> AppManager.getAppManager().finishActivity(this));
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        allMoney = bundle.getDouble(Constant.ALLMONEY);
        gmtTime = bundle.getString(Constant.GMT);
        couponInfos = (List<CouponInfo>) bundle.getSerializable(Constant.COUPONS);
        tvAllmoney.setText("当前消费金额" + turn(allMoney, Constant.DECIMAL_POINT) + "元");
        tvSelectcoupinsnum.setText("您当优惠券累计金额0元");
        setTitleBar(toolbar, "使用电子优惠券");
        calculateMoney();

        addData();
    }

    private void addData() {
        adapter = new SelectCouponsAdapter(couponInfos, allMoney, quanmoney, gmtTime);
        lvSelectcoupons.setAdapter(adapter);
    }

    private void calculateMoney() {
        quanmoney = 0;
        cutmoney = 0;
        for (CouponInfo couponInfo : couponInfos) {
            if (couponInfo.isChecked()) {
                quanmoney = quanmoney + couponInfo.getCard_upperlimit();
                cutmoney = cutmoney + couponInfo.getCard_value();
            }
        }
    }

    @OnClick(R.id.choose_over)
    public void onClick() {
        calculateMoney();
        if (quanmoney <= allMoney) {
            Intent intent = new Intent();
            intent.putExtra("money", cutmoney);
            intent.putExtra("newcoupons", (Serializable) couponInfos);
            setResult(1, intent);
            Log.e("Sm", quanmoney + "/////" + allMoney + "////" + cutmoney);
            AppManager.getAppManager().finishActivity(this);
        } else {
            toast("您使用的优惠券超出上限");
        }

    }
}
